﻿using ETA.IRepository;
using ETA.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETA.Service
{
    public class DocumentService : IDocumentService
    {

        #region 属性

        private IDocumentRepository _IDocumentRepository { set; get; }


        #endregion

        #region 构造函数

        public DocumentService(IDocumentRepository IDocumentRepository)
        {
            _IDocumentRepository = IDocumentRepository;
        }

        #endregion

        public QueryDocumentResponse QueryDocumentList(QueryDocumentRequest req)
        {
            var response = new QueryDocumentResponse();
            try
            {
                if (!string.IsNullOrEmpty(req.DocumentId))
                {
                    response.Documents = _IDocumentRepository.GetAllList(b => b.DocumentId == req.DocumentId);
                }
                else if (!string.IsNullOrEmpty(req.FileTypeId))
                {
                    response.Documents = _IDocumentRepository.GetAllList(b => b.UploadFile.FileType.FileTypeId == req.FileTypeId);
                }
                else
                {
                    response.Documents = _IDocumentRepository.GetAllList();
                }
                response.Result.IsSuccess = true;
            }
            catch (Exception e)
            {
                response.Result.Message = e.Message;
            }
            return response;
        }

        public CreateDocumentResponse CreateDocument(CreateDocumentRequest req)
        {
            var res = new CreateDocumentResponse();
            try
            {
                if (req != null)
                {
                    res.Document = _IDocumentRepository.Insert(req.Document);
                    res.Result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                res.Result.IsSuccess = false;
                res.Result.ErrMessage = e.Message;
            }
            return res;
        }

        public ModifyDocumentResponse ModifyDocument(ModifyDocumentRequest req)
        {
            var res = new ModifyDocumentResponse();
            try
            {
                if (req != null)
                {
                    _IDocumentRepository.Update(req.Document);
                    res.Result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                res.Result.IsSuccess = false;
                res.Result.ErrMessage = e.Message;
            }
            return res;
        }
    }
}
